package leetCode.firstWeek;

import java.util.LinkedList;
import java.util.List;

public class Code_36 {
    public boolean isValidSudoku(char[][] board) {
        List<Character> tmp;
        /**
         * 判断行
         */
        for (char[] i : board) {
            tmp = new LinkedList<>();
            for (char j : i) {
                tmp.add(j);
            }
            if (!isOnlyNine(tmp)) {
                return false;
            }
        }
        /**
         * 判断列
         */
        for (int i = 0; i < 9; i++) {
            tmp = new LinkedList<>();
            for (int j = 0; j < 9; j++) {
                tmp.add(board[j][i]);
            }
            if (!isOnlyNine(tmp)) {
                return false;
            }
        }
        /**
         * 判断9宫格
         */
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                tmp = new LinkedList<>();
                for (int k = 0; k < 3; k++) {
                    for (int l = 0; l < 3; l++) {
                        tmp.add(board[3 * i + k][3 * j + l]);
                    }
                }
                if (!isOnlyNine(tmp)) {
                    return false;
                }
            }
        }

        return true;

    }

    public boolean isOnlyNine(List<Character> one_nine) {
        List<Character> tmp = new LinkedList<>();
        for (Character i : one_nine) {
            if (tmp.contains(i) && i != '.') {
                return false;
            }
            tmp.add(i);
        }
        return true;
    }
}
